<?php
/**
 * BaseService.calss.php
 * 公共函数
 *
 */
namespace data\Service;
use Think\Session as Session;
class BaseService
{
    protected $uid;//会员ID
    protected $user_name;//会员名
    protected $store_id;  //店铺id
    protected $store_name;//店铺名称
    protected $is_admin;//后台管理员id
	protected $admin_name;//后台管理员名称

    public function __construct(){
        $this->init();
    }
    
    /**
     * 初始化数据
     */
    private function init(){
		
        $this->uid = $_SESSION['user_id'];
		$this->user_name = $_SESSION['user_name'];
		$this->store_id = $_SESSION['store_id'];
		$this->store_name = $_SESSION['store_name'];
		$this->is_admin = $_SESSION['is_admin'];
		$this->admin_name = $_SESSION['admin_name'];
    }
	 
	/*
	*字符串处理
	*/	
    public function strColation($str){
		$str = json_encode($str);
		$str = addslashes($str);	
		return $str ;
    }	

	/*
	*字符串解析
	*/	
    public function strAnalysis($str){
		$str = stripslashes($str);
		$str = json_decode($str);
		foreach ($str as $k=>$v) {
			if (is_object($v)) {
				$str [$k] = (array) $v;
			}
		}
		return $str ;		
    }
	
	/**
	 * 删除购物车记录
	 *
	 * @param array $buy_items 商品ID => 购买数量
	 */
	
	public function delCart($goods_id) {
		  $cart = $this->strAnalysis(session('_cart')) ;
		  foreach ($cart as $key => $v) {
			 if ($v['goods_id']===$goods_id) {
				 $del_num = $v['goods_num'] ;						
				 unset($v);
			 } else {
				 $amount += $v['amount'] ;
				 $cart_list [] = $v ;
			 }						
		 }
		 session('_cart',$this->strColation($cart_list)) ;
		 $num = session('_cart_goods_num') -  $del_num;
		 if ($num<0) {
			 $num = 0 ;
		 }
		 session('_cart_goods_num',$num) ;
		 session('_cart_goods_amount',$amount) ;			
	 }

	/**
	 * 删除购物车记录
	 *
	 * @param array $buy_items 商品ID => 购买数量
	 */
	
	public function delCookieCart($goods_id) {
		//浏览器保存购物车信息
		$cart = $this->strAnalysis(cookie('_cart'));
		foreach ($cart as $key => $v) {
			if ($v['goods_id']===$goods_id) {
				$del_num = $v['goods_num'] ;						
				unset($v);
			}
			$cart_list [$key] = $v ;
			$amount += $v['amount'] ;
			$amount_points += $v['amount_points'] ;
			$amount_coin += $v['amount_coin'] ;
		}
		cookie('_cart',$this->strColation($cart_list)) ;
		$num = cookie('_cart_goods_num') -  $del_num;
		if ($num<0) {
			$num = 0 ;
		}
		cookie('_cart_goods_num',$num) ;
		cookie('_cart_goods_amount',$amount) ;
		cookie('_cart_goods_amount_points',$amount_points) ;
		cookie('_cart_goods_amount_coins',$amount_coin) ;		
	 }	
}